package com.hancai.web;

import com.github.pagehelper.PageInfo;
import com.hancai.model.TBarrage;
import com.hancai.query.BarrageAndClientStatusQuery;
import com.hancai.result.R;
import com.hancai.service.BarrageService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author 涵菜
 * @version 1.0
 */
@RestController
public class BarrageController {

    @Resource
    private BarrageService barrageService;

    @GetMapping(value="/api/barrages")
    public R barragePage(@RequestParam(value = "current",required = false)Integer current , @RequestParam(value = "keyword",required = false)String keyword){
        if(current==null){
            current=1;
        }
        PageInfo<TBarrage> pageInfo = barrageService.getBarrageByPage(current,keyword);
        return R.OK(pageInfo);
    }


    /**
     * RequestBody注解获取请求体中的数据，适用于 POST 或 PUT 请求中的复杂数据传递。
     * @param
     * @param
     * @return
     */
    @PutMapping(value = "/api/barrage/changeStatus")
    public R changeStatus(@RequestBody BarrageAndClientStatusQuery barrageAndClientStatusQuery){
        // 获取参数
        Integer id = barrageAndClientStatusQuery.getId();
        Integer status = barrageAndClientStatusQuery.getStatus();
        barrageService.changeStatus(id,status);
        return R.OK();
    }

    @DeleteMapping(value = "/api/barrage/{id}")
    public R delBarrage(@PathVariable(value="id")Integer id){
        int del=barrageService.delBarrageById(id);
        return del>=1 ? R.OK() : R.FAIL();
    }

    @DeleteMapping(value = "/api/barrage")
    public R batchDelBarrage(@RequestParam(value="ids")String ids){
        List<String> idList= Arrays.asList(ids.split(","));
        int batchDel=barrageService.batchDelUserIds(idList);
        return batchDel>=idList.size() ? R.OK() : R.FAIL();
    }

    @PostMapping(value = "/api/importExcel")
    public R importExcel(MultipartFile file) throws IOException {
        barrageService.importExcel(file.getInputStream());
        return R.OK();
    }

}
